package 语法

/*
 *
 */
def map = new HashMap()

/**
 * groovy的定义map,和列表一样，只是每一个元素都有key:value
 * key默认是单引号不可变的字符串
 * 映射默认就是linkedHashMap如需修改就需要用as 或者定义时候直接指定，同列表一样
 */
def colors = [red:'ff0000',yellow:'00ff00']

/**
 * groovy根据key获取value,两种方式
 */
def var = colors['red']
println var
def var1 = colors.red
println var1
/**
 * 添加元素
 */
//这种就是先去查找是否存在key为blue的值，存在就修改，不存在就新增
colors.blue = 'ffff00'
println colors.toMapString()
//这个添加就是key是complex，value又是一个map
colors.complex=[a:1,b:2]
println colors.toMapString()
//删除元素就和java的删除一样，都是remove方法
println "----------------------------------------"


/**
 * 对映射的遍历。each和eachWithIndex
 * 可以直接遍历key和value，也可以直接遍历entry
 */
def students= ['1a':[number:'001',name:'Bob1',score:51,sex:'male'] ,
              '2b':[number:'002',name:'Bob2',score:62,sex:'female'] ,
              '3c':[number:'003',name:'Bob3',score:73,sex:'male'] ,
              '4d':[number:'004',name:'Bob4',score:84,sex:'female'] ,
            ]
//直接遍历entry
students.each {
    def student ->
        println "key ${student.key}"
        println "value ${student.value}"
}
//这个是直接遍历key和value
students.each{
    key,value ->
        println key
        println value
}
//带索引的遍历每一个entry,索引是从0开始的
students.eachWithIndex{
    def student , int index ->
        println "index ${index} key ${student.key}"
        println "value ${student.value}"
}
//带索引的遍历每一个key和value,索引是从0开始的
students.eachWithIndex{
   key,value, index ->
        println "index ${index} key ${key}"
        println "value ${value}"
}
println "-----------------------------------------------------------------"
/**
 * find方法,返回的是一个entry
 * findAll方法,返回的是一个Map
 * findAll+collect
 *any,every,count
 */
def find = students.find {
        //查找分数大于60的student
    def student -> return student.value.score > 60
}
println find
println find.class
println "-----------------------------------------------------------------"

def findAll = students.findAll {
    def student -> return student.value.score > 60
}
println findAll
println "-----------121212------------------------------------------------"
//得到所有及格学生的名字,返回的是一个list
def collectName = findAll.collect {
    student -> student.value.name
}
println collectName
println collectName.class
println "-----------------------------------------------------------------"

def every = students.every {
    def student -> student.value.score > 59
}
println every
println "-----------------------------------------------------------------"

def any = students.any {
    def student -> student.value.score>50
}
println any
println "-----------------------------------------------------------------"

def count = students.count {
    def student -> student.value.score>59
}
println count
println "-----------------------------------------------------------------"

/**
 * 分组
 */
def group = students.groupBy {
    //student -> student.value.score>60
     //如果没写默认的组名就是true和false，写了组名就是及格和不及格两组
    student -> student.value.score>60?'及格':'不及格'
}
println group
println "-----------------------------------------------------------------"

/**
 * map的排序,对立面的属性进行排序
 * map排完序返回一个新的map，原来的map不变
 * list排完序返回的是当前的list，只是当前的list变了
 */
def sort = students.sort {
    student1,student2 ->
        //这里没有用具体的类型，比如int，float，直接使用number进行操作
        Number score1= student1.value.score
        Number score2= student2.value.score
        return  score1 == score2?0:score1<score2?1:-1
}
println sort
println students


